package per.example.arpproject.utils;

import org.springframework.util.StreamUtils;


import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * @author yaozhang Li
 * @ TO DO
 * @creatTime 2022/6/10 17:17
 **/
public class RequestUtil {

    /**
     * @Description: TO DO 获取请求contentype信息
     * @Author: yaozhang Li
     * @Date: 2022/7/7 11:338
     * @param: 请求头
     * @return:
     *
     */
    public static String getContentType(HttpServletRequest request) {
        return request.getContentType();
    }

    /**
     * @Description: TO DO 获取请求ip信息
     * @Author: yaozhang Li
     * @Date: 2022/6/10 17:20
     * @param: 请求头
     * @return:
     *
     */
    public static String getIpAddr(HttpServletRequest request) {
        String ipAddress = null;
        try {
            ipAddress = request.getHeader("x-forwarded-for");
            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getHeader("Proxy-Client-IP");
            }
            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getHeader("WL-Proxy-Client-IP");
            }
            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getRemoteAddr();
                if (ipAddress.equals("127.0.0.1")) {
                    // 根据网卡取本机配置的IP
                    InetAddress inet = null;
                    try {
                        inet = InetAddress.getLocalHost();
                    } catch (UnknownHostException e) {
                        e.printStackTrace();
                    }
                    ipAddress = inet.getHostAddress();
                }
            }
            // 对于通过多个代理的情况，第一个IP为客户端真实IP,多个IP按照','分割
            if (ipAddress != null && ipAddress.length() > 15) {
                // = 15
                if (ipAddress.indexOf(",") > 0) {
                    ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
                }
            }
        } catch (Exception e) {
            ipAddress="";
        }

        return ipAddress;
    }

    /**
     * @Description: TO DO 获取请求体form表单参数信息
     * @Author: yaozhang Li
     * @Date: 2022/6/10 17:20
     * @param: 请求头
     * @return:
     *
     */
    public static String getBodyFormParam(HttpServletRequest request) throws IOException {
        Map map=request.getParameterMap();
        Set keSet=map.entrySet();
        StringBuffer sb = new StringBuffer();
        for(Iterator itr = keSet.iterator(); itr.hasNext();){
            Map.Entry me=(Map.Entry)itr.next();
            Object ok=me.getKey();
            Object ov=me.getValue();
            String[] value=new String[1];
            if(ov instanceof String[]){
                value=(String[])ov;
            }else{
                value[0]=ov.toString();
            }

            for(int k=0;k<value.length;k++){
                sb.append(ok+"="+value[k]+"&");
            }
        }
        if (sb.length() == 0) {
            return null;
        }
        return sb.toString().substring(0,sb.length()-1);
    }


    /**
     * @Description: TO DO 获取请求body，一次性，会破坏获取的InputStream
     * @Author: yaozhang Li
     * @Date: 2022/6/13 14:12
     * @param:
     * @return:
     *
     */
    public static String getBodyJsonParam(HttpServletRequest request) throws IOException {
        byte[] bodyBytes = StreamUtils.copyToByteArray(request.getInputStream());
        String body = new String(bodyBytes, request.getCharacterEncoding());
        return body;


    }

    /**
     * @Description: TO DO 获取url上的请求参数信息
     * @Author: yaozhang Li
     * @Date: 2022/6/10 17:54
     * @param:
     * @return:
     *
     */
    public static String getUrlParam(HttpServletRequest request) {
        String queryString = request.getQueryString();
        return queryString;
    }

    /**
     * @Description: TO DO 获取请求的url
     * @Author: yaozhang Li
     * @Date: 2022/6/10 17:53
     * @param:
     * @return:
     *
     */
    public static String getUrl(HttpServletRequest request) {
        return request.getRequestURI();
    }

    /**
     * @Description: TO DO 获取请求方法
     * @Author: yaozhang Li
     * @Date: 2022/6/13 14:16
     * @param:
     * @return:
     *
     */
    public static String getMethod(HttpServletRequest request) {
        return request.getMethod();
    }

}
